package com.jblog.controller.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jblog.controller.front.action.BaseAct;

@Controller
@RequestMapping("/jvm")
public class Jvm extends BaseAct {
	private jvm jvm;
	private final double MB = 1.0 * 1024 * 1024;

	@RequestMapping(method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	public String index(HttpServletRequest request, ModelMap model) {
		this.jvm = new jvm();
		Runtime rt = Runtime.getRuntime();

		this.jvm.setMax(rt.maxMemory() / MB);
		this.jvm.setTotal(rt.totalMemory() / MB);
		this.jvm.setFree(rt.freeMemory() / MB);

		model.put("jvm", this.jvm);
		return O(request, model, "main/dialog/jvm");
	}

	public jvm getJvm() {
		return this.jvm;
	}

	public void setJvm(jvm jvm) {
		this.jvm = jvm;
	}

	public static class jvm {
		private double max;
		private double total;
		private double free;
		@SuppressWarnings("unused")
		private double actual;

		public double getMax() {
			return this.max;
		}

		public void setMax(double max) {
			this.max = max;
		}

		public double getTotal() {
			return this.total;
		}

		public void setTotal(double total) {
			this.total = total;
		}

		public double getFree() {
			return this.free;
		}

		public void setFree(double free) {
			this.free = free;
		}

		public double getActual() {
			return (this.max - this.total + this.free);
		}

		public String getMaxr() {
			return to(this.max);
		}

		public String getTotalr() {
			return to(this.total);
		}

		public String getFreer() {
			return to(this.free);
		}

		public String getActualr() {
			return to(getActual());
		}

		private Double round(double d, int decimal) {
			Double result = Double.valueOf(-0.1D);
			try {
				double factor = Math.pow(10.0D, decimal);
				result = Double.valueOf(Math.floor(d * factor + 0.5D) / factor);
			} catch (Exception e) {
				return result;
			}
			return result;
		}

		private String to(double d) {
			return round(d, 2).toString();
		}
	}
}